package com.huawei.hianalytics.f.f;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import com.huawei.appgallery.serverreqkit.api.bean.ResponseBean;
import com.huawei.hianalytics.a.b;
import com.huawei.hianalytics.f.g.e;
import com.huawei.hianalytics.f.g.j;
import com.huawei.hianalytics.h.c;
import com.huawei.hianalytics.util.f;
import com.huawei.hianalytics.util.h;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class p {
    private Context a;

    p(Context context) {
        this.a = context;
    }

    public void a() {
        int i = 0;
        if (b.h()) {
            List a = e.a(this.a);
            com.huawei.hianalytics.h.e.a().a("pre_report_url_tag", j.a(a, "{url}/getServerInfoWithBackup"));
            CharSequence a2 = f.a("ro.product.CustCVersion", "");
            com.huawei.hianalytics.g.b.b("HiAnalytics/event", "cust version: %s", a2);
            if (!TextUtils.isEmpty(a2)) {
                try {
                    JSONArray jSONArray;
                    JSONObject jSONObject = new JSONObject();
                    jSONObject.put("isoCode", a2);
                    byte[] bytes = jSONObject.toString().getBytes("UTF-8");
                    c a3 = com.huawei.hianalytics.h.e.a().a(bytes, null, "pre_report_url_tag");
                    if (a3.a() == ResponseBean.NOT_FOUND) {
                        com.huawei.hianalytics.g.b.b("ServerAddrGetTask", "second time to get the pro address");
                        com.huawei.hianalytics.h.e.a().a("pre_report_backup_url_tag", j.a(a, "{url}/getServerInfo"));
                        a3 = com.huawei.hianalytics.h.e.a().a(bytes, null, "pre_report_backup_url_tag");
                    }
                    JSONObject jSONObject2 = new JSONObject(a3.b());
                    String optString = jSONObject2.optString("resultcode", "-100");
                    com.huawei.hianalytics.g.b.b("HiAnalytics/event", "get server add response err code: %s", optString);
                    List arrayList = new ArrayList();
                    JSONArray optJSONArray;
                    if (jSONObject2.has("serverUrls")) {
                        optJSONArray = jSONObject2.optJSONArray("serverUrls");
                        if (optJSONArray != null) {
                            int length = optJSONArray.length();
                            while (i < length) {
                                CharSequence optString2 = optJSONArray.optString(i, "");
                                if (h.a(optString2)) {
                                    arrayList.add("{url}/common/hmshioperbatch".replace("{url}", optString2));
                                }
                                i++;
                            }
                            jSONArray = optJSONArray;
                        } else {
                            jSONArray = optJSONArray;
                        }
                    } else {
                        CharSequence optString3 = jSONObject2.optString("serverUrl", "");
                        if (h.a(optString3)) {
                            arrayList.add("{url}/common/hmshioperbatch".replace("{url}", optString3));
                        }
                        optJSONArray = new JSONArray();
                        optJSONArray.put(optString3);
                        jSONArray = optJSONArray;
                    }
                    String[] strArr = (String[]) arrayList.toArray(new String[arrayList.size()]);
                    if (strArr.length <= 0 || jSONArray == null) {
                        com.huawei.hianalytics.g.b.c("HiAnalytics/event", "ServerAddrGetTask() No access to preloaded URL");
                        return;
                    }
                    b.a(strArr);
                    b.a(false);
                    com.huawei.hianalytics.h.e.a().a("preload_url_tag", strArr);
                    SharedPreferences d = com.huawei.hianalytics.f.g.h.d(this.a, "global_v2");
                    com.huawei.hianalytics.f.g.h.a(d, "upload_url", jSONArray.toString());
                    com.huawei.hianalytics.f.g.h.a(d, "upload_url_time", Long.valueOf(System.currentTimeMillis()));
                    return;
                } catch (JSONException e) {
                    com.huawei.hianalytics.g.b.c("ServerAddrGetTask", "getUploadServerAddr(): JSON structure Exception!");
                    return;
                } catch (UnsupportedEncodingException e2) {
                    com.huawei.hianalytics.g.b.c("ServerAddrGetTask", "get server address Unsupported Encoding: UTF-8!");
                    return;
                }
            }
            return;
        }
        com.huawei.hianalytics.g.b.c("HiAnalytics/event", "ServerAddrGetTask() Not need RetrieveUploadUrl,URL is empty, But the switch is closed !");
    }
}
